package com.corpdata.app.customer.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.corpdata.core.base.AbstractBaseService;
import com.corpdata.core.base.BaseEntity;
import com.corpdata.system.security.shiro.util.UserUtil;
import com.corpdata.app.customer.dao.CustomerIntegralMapper;
import com.corpdata.app.customer.entity.CustomerIntegral;
import com.corpdata.app.customer.entity.CustomerTeam;
import com.corpdata.app.customer.service.CustomerIntegralService;
import com.corpdata.app.customer.service.CustomerTeamService;
import com.corpdata.app.project.service.ProjectStationService;
import com.corpdata.common.result.Result;
import com.corpdata.common.utils.CorpdataUtil;

/**
 * 团队积分
 * 
 * @author wu
 * @email 64088839@qq.com
 * @date 2018-07-25 15:51:07
 */
@Service
@Transactional
public class CustomerIntegralServiceImpl extends AbstractBaseService<CustomerIntegral> implements CustomerIntegralService {
	
	@Autowired
	CustomerIntegralMapper integralMapper;
	
	@Autowired
	ProjectStationService psServ;
	
	@Autowired
	CustomerTeamService teamServ;
	
	public Result insert(CustomerIntegral record){
		CustomerIntegral ci=super.findById(record.getFkTeam());
		if(record.getType()==1){
			if(ci!=null){
				record.setGiftBalance(ci.getGiftBalance());
				record.setExistingBalance(record.getBaseRecharge()+ci.getExistingBalance());
				if(ci.getBaseBalance()!=null){
					record.setBaseBalance(record.getBaseRecharge()+ci.getBaseBalance());
				}else{
					record.setBaseBalance(record.getBaseRecharge());
				}
			}else{
				record.setGiftBalance(0);
				record.setBaseBalance(record.getBaseRecharge());
				record.setExistingBalance(record.getBaseBalance());
			}
		}else if(record.getType()==2){
			if(ci!=null){
				
				record.setBaseBalance(ci.getBaseBalance());
				record.setExistingBalance(record.getGiftRecharge()+ci.getExistingBalance());
				if(ci.getGiftBalance()!=null){
					record.setGiftBalance(record.getGiftRecharge()+ci.getGiftBalance());
				}else{
					record.setGiftBalance(record.getGiftRecharge());
				}
			}else{
				record.setBaseBalance(0);
				record.setGiftBalance(record.getGiftRecharge());
				record.setExistingBalance(record.getGiftRecharge());
			}
			
			record.setRemarks("	赠送"+record.getGiftRecharge()+"积分");
		}else if(record.getType()==3){
			if(ci!=null){
				record.setBaseBalance(ci.getBaseBalance());
				record.setExistingBalance(ci.getExistingBalance()-record.getGiftRecharge());
				record.setGiftBalance(ci.getGiftBalance()-record.getGiftRecharge());
				record.setRemarks("收回"+record.getGiftRecharge()+"赠送积分");
			}
		}
		return super.save(record);
	}
	
	public Result orderInsert(CustomerIntegral record){
		CustomerIntegral ci=super.findById(record.getFkTeam());
		if(ci!=null){
			record.setBaseBalance(ci.getBaseBalance());
			record.setExistingBalance(ci.getExistingBalance()-record.getGiftRecharge());
			record.setGiftBalance(ci.getGiftBalance()-record.getGiftRecharge());
			record.setRemarks("预定会议室花费"+record.getGiftRecharge()+"积分");
		}
		return super.save(record);
	}

	/**
	 * 月初积分重新分配
	 */
	@Override
	public void pointsReAllot() {
		int stationCount = 0;
		int score = 0;
		//查询所有在租团队列表
		List<CustomerTeam> teamList = teamServ.selectByPid(null);
		for(CustomerTeam t:teamList){
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("fkTeam", t.getId());
			params.put("zaizu", 1);
			//获取团队在租合同的工位数
			stationCount = psServ.getStationCount(params);
			score = stationCount*380;
			CustomerIntegral record=super.findById(t.getId());
			if(record != null){
				record.setExistingBalance(score+record.getExistingBalance()-record.getGiftBalance());
			}else{
				record = new CustomerIntegral();
				record.setBaseBalance(0);
				record.setExistingBalance(score);
			}
			record.setFkTeam(t.getId());
			record.setGiftBalance(score);
			record.setGiftRecharge(score);
			record.setType(2);
			record.setId(CorpdataUtil.getUUID());
			record.setRemarks("	赠送"+score+"积分");
			Date date = new Date();
			record.setCreated(date);
			record.setModified(date);
			integralMapper.insert(record);
		}
			
		
	}
	
	
}
